package ru.papergames.battleserver.util.pathfinding.heuristic;

import ru.papergames.battleserver.model.battle.npc.NpcBattleMap;
import ru.papergames.battleserver.util.pathfinding.AStarHeuristic;

public class GoToDistanceHeuristic implements AStarHeuristic {
	/** The minimum movement cost from any one square to the next */
	private int minimumCost;
	private int distance;

	/**
	 * Create a new heuristic
	 *
	 * @param minimumCost The minimum movement cost from any one square to the next
	 */
	public GoToDistanceHeuristic(int minimumCost, int distance) {
		this.minimumCost = minimumCost;
		this.distance = distance;
	}

	/**
	 * @see AStarHeuristic#getCost(NpcBattleMap , int, int, int, int)
	 */
	public int getCost(NpcBattleMap map, int x, int y, int tx, int ty) {
        int curDist = calculateDistance(x, y, tx, ty);

		return minimumCost * (distance - curDist);
	}

    public static int calculateDistance(int x1, int y1, int x2, int y2) {
        int dx = x1 - x2;
        int dy = y1 - y2;
        dx = (dx < 0) ? -dx : dx;
        dy = (dy < 0) ? -dy : dy;
        return dx > dy ? dx : dy;
    }

}
